package com.peter.configurationchangedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.ViewTreeObserver;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tv);


        ViewTreeObserver vto = tv.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int height = tv.getMeasuredHeight();
                int width = tv.getMeasuredWidth();

                Log.e(TAG, "onPreDraw: " + width + "    " + height, null);

                return true;
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub

        // hen 2400(kuan) 1080(gao)  fenping 1140(kuan)    1080(gao)
//        I/System.out: DisplayMetrics{density=2.75, width=2309, height=1036, scaledDensity=2.75, xdpi=394.705, ydpi=394.716}

        // shu   1080 2400  onPreDraw: 1080(kuan)    1653  fenping 1080    1209  1080    1721
//        I/System.out: DisplayMetrics{density=2.75, width=1080, height=2265, scaledDensity=2.75, xdpi=394.705, ydpi=394.716}
        getPingMuSize(this);

        Log.e("config", "newConfig.screenHeightDp:" + newConfig.screenHeightDp
                + ", newConfig.screenWidthDp" + newConfig.screenWidthDp);

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            tv.setText("现在是竖屏");
//            W/config: newConfig.screenHeightDp:823, newConfig.screenWidthDp392
        }

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            tv.setText("现在是横屏");
//            W/config: newConfig.screenHeightDp:343, newConfig.screenWidthDp839

        }

        // 小窗 无

        // 分屏 heng
//        W/config: newConfig.screenHeightDp:343, newConfig.screenWidthDp414

        // 分屏 竖
        // W/config: newConfig.screenHeightDp:406, newConfig.screenWidthDp392

        super.onConfigurationChanged(newConfig);
    }

    private static final String TAG = MainActivity.class.getName();


    /**
     * @ 获取当前手机屏幕的尺寸(单位:像素)
     */
    public float getPingMuSize(Context mContext) {
        int densityDpi = mContext.getResources().getDisplayMetrics().densityDpi;
        float scaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
        float density = mContext.getResources().getDisplayMetrics().density;
        float xdpi = mContext.getResources().getDisplayMetrics().xdpi;
        float ydpi = mContext.getResources().getDisplayMetrics().ydpi;
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        int height = mContext.getResources().getDisplayMetrics().heightPixels;


        System.out.println(mContext.getResources().getDisplayMetrics());

        // 这样可以计算屏幕的物理尺寸
        float width2 = (width / xdpi) * (width / xdpi);
        float height2 = (height / ydpi) * (width / xdpi);


        // 通过WindowManager获取
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        System.out.println("width-display :" + dm.widthPixels);
        System.out.println("heigth-display :" + dm.heightPixels);

        // 通过Resources获取
        DisplayMetrics dm2 = getResources().getDisplayMetrics();
        System.out.println("width-display :" + dm2.widthPixels);
        System.out.println("heigth-display :" + dm2.heightPixels);

        // 获取屏幕的默认分辨率
        Display display = getWindowManager().getDefaultDisplay();
        System.out.println("width-display :" + display.getWidth());
        System.out.println("heigth-display :" + display.getHeight());


        return (float) Math.sqrt(width2 + height2);
    }

}